using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library.Components.Security;
using Framework.Library.Components.Commands;
using Framework.Library;
using Framework.Library.Utilities.Database;
using Framework.Library.Utilities;
using Framework.Library.Components.Database;

namespace Framework.Portal.Administrator.Database
{
    public partial class TablesAction : AuthenticatedControl
    {
        _Table objTable = new _Table();       
        #region "Drop"

        private void loadDropForm()
        {
            pnlDrop.Visible = true;
            lblDropTableName.Text = objTable.Owner + "." + objTable.Name;
        }

        protected void btnDrop_Click(object sender, EventArgs e)
        {
            try
            {
                objTable.ID = Convert.ToInt64(Request["id"]);
                objTable.LoadProperties();

                objTable.Drop();
                Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.Table)));
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
                lblError.Text = "<p>" + ex.Message + "</p>";
            }
        }

        #endregion

        #region "Rename"

        private void loadRenameForm()
        {
            pnlRename.Visible = true;
            lblRenameTable.Text = objTable.Owner + "." + objTable.Name;
            lblOwner.Text = objTable.Owner;
        }

        protected void btnRename_Click(object sender, EventArgs e)
        {
            try
            {
                objTable.ID = Convert.ToInt64(Request["id"]);
                objTable.LoadProperties();
                objTable.Rename(txtNewName.Text);
                Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.Table)));
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
                lblError.Text = "<p>" + ex.Message + "</p>";
            }
        }

        #endregion

        void Page_Load(object sender, EventArgs e)
        {
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
            if (!IsPostBack)
            {                
                objTable.ID = Convert.ToInt64(Request["id"]);
                objTable.LoadProperties();

                //switch on action
                switch (Request.QueryString["action"].ToString())
                {
                    case "drop":                        
                        loadDropForm();
                        break;
                    case "rename":
                        loadRenameForm();
                        break;
                }
                lnkTableList.NavigateUrl = AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.Table));
                lnkTableDetails.NavigateUrl = AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.TableProperty) + "&id=" + Request.QueryString["id"].ToString());
            }
        }
    }
}